package cn.xz.zysjz.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.Objects;

import cn.xz.zysjz.MainActivity;
import cn.xz.zysjz.R;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.UserBean;
import cn.xz.zysjz.databinding.ActivityLoginBinding;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.GetDataUtils;
import cn.xz.zysjz.utils.SessionUtils;
import cn.xz.zysjz.utils.ToastUtils;

/**
 * 登陆界面*/
public class LoginActivity extends AppCompatActivity {

    ActivityLoginBinding mBinding;

    boolean admin  = false; //当前是否是管理员登陆

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        if (Build.VERSION.SDK_INT >= 21) {
            this.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        initEvent();
        requestPermission();
    }

    private void initEvent(){
        //点击用户
        mBinding.tvUser.setOnClickListener(v->{
            admin = false;
            mBinding.tvUser.setTextColor(Color.parseColor("#000000"));
            mBinding.tvAdmin.setTextColor(Color.parseColor("#999999"));
        });
        //点击管理员
        mBinding.tvAdmin.setOnClickListener(v->{
            admin = true;
            mBinding.tvAdmin.setTextColor(Color.parseColor("#000000"));
            mBinding.tvUser.setTextColor(Color.parseColor("#999999"));
        });
        //点击登陆
        mBinding.tvDl.setOnClickListener(v->{
            CommonLib.isAdmin = false;
            String name = mBinding.etName.getText().toString();
            String pwd = mBinding.etPwd.getText().toString();
            if (name.equals("") || pwd.equals("")){
                ToastUtils.show("请输入用户名或者密码");
                return;
            }
            if (admin){
                //管理员登陆
                if (name.equals("admin") && pwd.equals("admin")){
                    AppHelper.toJump(MainActivity.class);
                    CommonLib.isAdmin = true;
                    finish();
                }else {
                    ToastUtils.show("当前管理员不存在");
                }
            }else {
                UserBean userBean = GetDataUtils.getUser(name);
                if (userBean == null) {
                    ToastUtils.show("该用户不存在");
                }else {
                    if (!Objects.equals(userBean.getPwd(), pwd)){
                        ToastUtils.show("密码错误");
                    }else {
                        if (userBean.getPass() != null && userBean.getPass()){
                            CommonLib.nowUserBean = userBean;
                            AppHelper.toJump(MainActivity.class);
                            finish();
                        }else {
                            ToastUtils.show("该用户在审核中，请耐心等待");
                        }
                    }
                }
            }

        });
        //点击注册
        mBinding.tvZc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.toJump(ZCActivity.class);
            }
        });
    }


    private boolean isPermissionRequested = false;
    public void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23 && !isPermissionRequested) {
            isPermissionRequested = true;
            ArrayList<String> permissionsList = new ArrayList<>();
            String[] permissions = {//在这里加入你要使用的权限
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            };

            for (String perm : permissions) {
                if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(perm)) {
                    permissionsList.add(perm);
                    // 进入这里代表没有权限.
                }
            }

            if (!permissionsList.isEmpty()) {
                String[] strings = new String[permissionsList.size()];
                requestPermissions(permissionsList.toArray(strings), 0);
            }
        }
    }

}